﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RedHouse.Data.Service;
using RedHouse.Data.Base;
using RedHouse.Data.Common;

namespace RedHouse.AdminControl.UserControls
{
    public partial class UCSlideShowDetail : System.Web.UI.UserControl
    {
        public string Message { get; set; }
        private ILocationService _locationService;
        private ILanguageService _languageService;
        private ISlideShowService _slideShowService;
        public ISlideShowService SlideShowService
        {
            get
            {
                if (_slideShowService == null)
                {
                    _slideShowService = new SlideShowService();
                }
                return _slideShowService;
            }
        }
        private int _id = -1;
        protected void Page_Load(object sender, EventArgs e)
        {
            _languageService = new LanguageService();
            _locationService = new LocationService();            

            if (!IsPostBack)
            {
                Initialize();
            }

            btnSave.Click += new EventHandler(btnSave_Click);
        }

        void btnSave_Click(object sender, EventArgs e)
        {
            RedHouse.Data.SlideShow slideShow = GetSlideShowFromView();
            OperationResult result ;
            int id = -1;
            int.TryParse(hdfId.Value, out id);
            if (id > 0)
            {
                slideShow.SlideShowId = int.Parse(hdfId.Value);
                result = SlideShowService.UpdateSlideShow(slideShow);
            }
            else
            {
                result = SlideShowService.SaveSlideShow(slideShow);
            }
            ErrorPanel.Visible = !result.Result;
            SuccessPanel.Visible = result.Result;
            if (result.Result)
            {
                Reset();
                Response.Redirect("SlideShow.aspx?action=2");
            }
        }

        private void Reset()
        {
            _id = -1;
            txtTitle.Text = string.Empty;
            txtDesc.InnerText = string.Empty;
            txtImageUrl.Text = string.Empty;
            txtReferenceUrl.Text = string.Empty;
            txtSmallImageUrl.Text = string.Empty;
            chkIsDisplayed.Checked = false;
        }

        private RedHouse.Data.SlideShow GetSlideShowFromView()
        {
            RedHouse.Data.SlideShow ret = new RedHouse.Data.SlideShow()
            {
                Tile = txtTitle.Text,
                LocationId = int.Parse(cboLocation.SelectedValue),
                LanguageId =new Guid(cboLanguage.SelectedValue),
                FullSizeImageUrl = txtImageUrl.Text,
                ThumbImageUrl = txtSmallImageUrl.Text,
                ReferenceUrl = txtReferenceUrl.Text,
                IsDisplayed = chkIsDisplayed.Checked,
                Description = txtDesc.InnerText
            };
            return ret;
        }

        private void Initialize()
        {
            Message = string.Empty;
            ErrorPanel.Visible = false;
            SuccessPanel.Visible = false;

            //Databinding
            cboLocation.DataSource = _locationService.GetAllLocation();
            cboLocation.DataTextField = "Name";
            cboLocation.DataValueField = "ID";
            cboLocation.DataBind();

            cboLanguage.DataSource = _languageService.GetAllLanguage();
            cboLanguage.DataTextField = "Description";
            cboLanguage.DataValueField = "ID";
            cboLanguage.DataBind();
        }

        public void BindSlideShowDetail(int id)
        {
            RedHouse.Data.SlideShow slideShow = SlideShowService.GetSlideShowById(id);
            if (slideShow != null)
            {
                cboLanguage.SelectedValue = slideShow.LanguageId.ToString();
                cboLocation.SelectedValue = slideShow.LocationId.ToString();
                txtDesc.InnerText = slideShow.Description;
                txtImageUrl.Text = slideShow.FullSizeImageUrl;
                txtSmallImageUrl.Text = slideShow.ThumbImageUrl;
                txtReferenceUrl.Text = slideShow.ReferenceUrl;
                txtTitle.Text = slideShow.Tile;
                chkIsDisplayed.Checked = slideShow.IsDisplayed.Value;
                hdfId.Value = slideShow.SlideShowId>0?slideShow.SlideShowId.ToString():"-1";
            }
        }

    }
}